package org.nci.platform.workflow.service;

import org.flowable.common.engine.impl.db.DbSqlSession;
import org.flowable.engine.ManagementService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Consumer;
import java.util.function.Function;

@Component
public class FlowableMapperService {

    @Resource
    private ManagementService managementService;

    // 通用的查询执行方法
    public <T, R> R executeQuery(Class<T> mapperClass, Function<T, R> operation) {
        return managementService.executeCommand(commandContext -> {
            T mapper = commandContext.getSession(DbSqlSession.class).getCustomMapper(mapperClass);
            return operation.apply(mapper);
        });
    }

    // 通用的更新执行方法
    public <T> void executeUpdate(Class<T> mapperClass, Consumer<T> operation) {
        managementService.executeCommand(commandContext -> {
            T mapper = commandContext.getSession(DbSqlSession.class).getCustomMapper(mapperClass);
            operation.accept(mapper);
            return null;
        });
    }

}